; *** Nine Men's Morris
; *** Copyright 1998-2002 Zillions Development
; v.2.0

; You need to purchase Zillions of Games to load this rules file
; Visit the Zillions web site at http://www.zillions-of-games.com

(version "2.0")
(translate
 ("Five Men's Morris" "五子直棋") ("Six Men's Morris" "六子直棋")
 ("Nine Men's Morris" "九子直棋") ("Twelve Men's Morris" "十二子直棋")
 ("Black" "黑方")("Red" "红方") ("man" "棋子")
)

; Each of two players has nine men of the same color.  With alternate moves
; they place their men at vacant points.  When all men have been placed,
; play continues by players taking turns to move a piece to an adjacent
; vacant space along one of the lines.  The first player to capture all but
; two opponent men is the winner.  Mill forming (three in a row) lets
; you capture any opponent piece not already in a mill. Stalemate is a draw.
; flag eatit is used to indicate if this move needs to be written (true) or already has
;   been written by the capture logic
; flag mill is used to indicate a mill has been formed, and a piece may be capturable
; flag writeit is used to indicate if the possible capture piece itself is in a mill

(define not-in-a-mill (set-flag eatit true) (if (on-board? $1) $1) 
      (if (on-board? $1) $1)
      (if enemy? (if (on-board? $2) $2
	   (if enemy? (if (on-board? $2) $2 
	    (if enemy? (set-flag eatit false)))))))

;used in 6 and 9 Men Morris
(define take-piece a1 (while (on-board? next) 
					 (if enemy?  mark 
					 (not-in-a-mill n s) back (if (flag? eatit) 
                       (not-in-a-mill e w) back (if (flag? eatit) 
					      capture add (set-flag writeit false) ) ) 
						  ) next) )
; used in 12 Men Morris
(define take-piece2 a1 (while (on-board? next) 
					 (if enemy?  mark 
					 (not-in-a-mill n s) back (if (flag? eatit) 
                       (not-in-a-mill e w) back (if (flag? eatit) 
					    (not-in-a-mill in out) back (if (flag? eatit)
					      capture add (set-flag writeit false) ) ) )
						  ) next) )
(define take-any a1 (while (on-board? next) 
					 (if enemy? 
					      capture add (set-flag writeit false))  next) )
						  
;three versions (offsets) of checking for mills
(define check-dir1 (if (on-board? $1) $1 (if (on-board? $1) $1 (if friend? $2 (if friend? (set-flag mill true))))))
(define check-dir2 (if (on-board? $1) (if (on-board? $2) $1 (if friend? $2 $2 (if friend? (set-flag mill true))))))
(define check-dir3 (if (on-board? $2) $2 (if (on-board? $2) $2 (if friend? $1 (if friend? (set-flag mill true))))))

;check of this position is in a mill
(define check-mill (set-flag mill false) 
        (check-dir1 n s)
		back
		(check-dir2 n s)
		back
		(check-dir3 n s)
		back
		(check-dir1 e w)
		back
		(check-dir2 e w)
		back
		(check-dir3 e w)
		back
		to
		(if (flag? mill) (take-piece)
		  (if (flag? writeit) (take-any)))
)

;used in 12 Men Morris
(define check-mill2 (set-flag mill false) 
        (check-dir1 n s)
		back
		(check-dir2 n s)
		back
		(check-dir3 n s)
		back
		(check-dir1 e w)
		back
		(check-dir2 e w)
		back
		(check-dir3 e w)
		back
		(check-dir1 in out)
	    back
	    (check-dir2 in out)
	    back
	    (check-dir3 in out)
	    back
		to
		(if (flag? mill) (take-piece2)
		  (if (flag? writeit) (take-any))
		)
)

(define check-milln (set-flag mill false) 
		(check-dir1 e w)
		back
		(check-dir2 e w)
		back
		(check-dir3 e w)
		back
		to
		(if (flag? mill) (take-piece)
		  (if (flag? writeit) (take-any)))
)
;used in 12 Men Morris
(define check-mille2 (set-flag mill false) 
        (check-dir1 n s)
		back
		(check-dir2 n s)
		back
		(check-dir3 n s)
		back
		(check-dir1 in out)
	    back
	    (check-dir2 in out)
	    back
	    (check-dir3 in out)
	    back
		to
		(if (flag? mill) (take-piece2)
		  (if (flag? writeit) (take-any))
		)
)
;used in 12 Men Morris
(define check-milli2 (set-flag mill false) 
        (check-dir1 n s)
		back
		(check-dir2 n s)
		back
		(check-dir3 n s)
		back
		(check-dir1 e w)
		back
		(check-dir2 e w)
		back
		(check-dir3 e w)
		back
		to
		(if (flag? mill) (take-piece2)
		  (if (flag? writeit) (take-any))
		)
)
;used in 12 Men Morris
(define check-milln2 (set-flag mill false) 
		(check-dir1 e w)
		back
		(check-dir2 e w)
		back
		(check-dir3 e w)
		back
		(check-dir1 in out)
	    back
	    (check-dir2 in out)
	    back
	    (check-dir3 in out)
	    back
		to
		(if (flag? mill) (take-piece2)
		  (if (flag? writeit) (take-any))
		)
)

(define check-mille (set-flag mill false) 
        (check-dir1 n s)
		back
		(check-dir2 n s)
		back
		(check-dir3 n s)
		back
		to
		(if (flag? mill) (take-piece)
		  (if (flag? writeit) (take-any)))
)

(define mill-add (check-mill))
(define mill-add2 (check-mill2))
(define mill-addn (check-milln))
(define mill-adde (check-mille))
(define mill-addn2 (check-milln2))
(define mill-adde2 (check-mille2))
(define mill-addi2 (check-milli2))

(define shiftn        ($1 mark (verify empty?) (set-flag writeit true) (mill-addn) (if (flag? writeit) add))) 
(define shifte        ($1 mark (verify empty?) (set-flag writeit true) (mill-adde) (if (flag? writeit) add))) 
(define shiftn2       ($1 mark (verify empty?) (set-flag writeit true) (mill-addn2) (if (flag? writeit) add))) 
(define shifte2       ($1 mark (verify empty?) (set-flag writeit true) (mill-adde2) (if (flag? writeit) add))) 
(define shifti2       ($1 mark (verify empty?) (set-flag writeit true) (mill-addi2) (if (flag? writeit) add))) 

(define shift         ($1 mark (verify empty?) (set-flag writeit true) (mill-add2) (if (flag? writeit) add))) 
(define add-to-empty  (mark (verify empty?) (verify (not-position? last))
                        (set-flag writeit true) (mill-add) (if (flag? writeit) add)))

(define add-to-empty2  (mark (verify empty?) (verify (not-position? last))
                        (set-flag writeit true) (mill-add2) (if (flag? writeit) add)))

(define positions-1-2
    (a1 199 37  234 70)
    (a2 199 91  234 124)
    (b1 361 37  397 70)
    (b2 306 91  341 124)
    (c1 361 201 397 233)
    (c2 306 201 341 233)
    (d1 361 363 397 396)
    (d2 306 309 341 342)
    (e1 199 363 234 396)
    (e2 199 309 234 342)
    (f1 36  363 72  396)
    (f2 92  309 127 342)
    (g1 36  200 72  232)
    (g2 92  200 127 232)
    (h1 36  37  72  70)
    (h2 92  91  127 124)
)
(define positions-3
    (a3 199 144 234 178)
    (b3 252 144 289 178)
    (c3 252 201 289 233)
    (d3 252 255 289 289)
    (e3 199 255 234 289)
    (f3 144 255 181 289)
    (g3 144 200 181 232)
    (h3 144 144 181 178)
)

(game
        (title "Five Men's Morris")
		(description "棋子须先落在棋盘上，方可按线移动。\\
		任何时候，若你走成三子一线，你可以提走对方一个棋子。若对方也有一些子成三子一线，则这些子不能被提吃。\\
		让对方无路可走，或让对方只剩下两个棋子，即可获胜。\\五子直棋每方有5个棋子，六子直棋有6个……以此类推。")
        (history "这种变体因趣味性不高，17世纪已经被淘汰。")
        (strategy "开局基本上决定了整局游戏。5、6子直棋先手优势很大，但还是需要考虑一些策略性。
                   开局成三不一定是好事，有可能因为难以移动而落后。形成双三（一个子来回移动连续成三）就是必胜了。")
        (option "prevent flipping" 2)
        (option "include off-pieces" true)
        (click-sound "Audio\\WoodClick.wav")
        (release-sound "Audio\\WoodClick.wav")
        (move-sound "Audio\\WoodClick.wav")
        (players Black Red)
        (turn-order Black Red)
        (move-priorities dropping moving)
		(board
            (image "images/Morris/6_Mens_Morris.bmp")
            (links n (a2 a1) (e1 e2) (d1 c1) (c1 b1)
			         (d2 c2) (c2 b2) (f1 g1) (g1 h1) (f2 g2) (g2 h2)
		    )
            (links e (h1 a1) (a1 b1) (h2 a2) (a2 b2) (c2 c1)  
					 (f2 e2) (e2 d2) (f1 e1) (e1 d1) (g1 g2)
		    )
		    (links s (a1 a2) (e2 e1) (c1 d1) (b1 c1)
			         (c2 d2) (b2 c2) (g1 f1) (h1 g1) (g2 f2) (h2 g2)
		    )
            (links w (a1 h1) (b1 a1) (a2 h2) (b2 a2)
			         (g2 g1) (c1 c2) 
					 (e2 f2) (d2 e2) (e1 f1) (d1 e1)
		    )
			(links next (a1 a2) (a2 b1) (b1 b2) (b2  c1)
			         (c1 c2) (c2 d1) (d1 d2) (d2 e1) (e1 e2) 
					 (e2 f1) (f1 f2)
					 (f2 g1) (g1 g2) (g2 h1)
					 (h1 h2) (h2 last)
		    )
            (positions
				(positions-1-2)
				(last -200 -200 -100 -100)	
		)
     )
	(board-setup
            (Red (man off 5))
            (Black (man off 5))
	)	
        (piece
            (name man)
		    (image Red   "images/Morris/NMMRedZ.bmp"   "images/Morris/NMMRed.bmp"
		           Black "images/Morris/NMMBlackZ.bmp" "images/Morris/NMMBlack.bmp")
		    (help "Man: First drop to any square on the board, then move along the lines.")
            (drops
                (move-type dropping)
                (add-to-empty)
            )
		    (moves
		       (move-type moving)
		        (shiftn n)
		        (shifte e)
		        (shiftn s)
		        (shifte w)
            )
        )
	   
        (loss-condition (Red Black) (pieces-remaining 2 man))	
	(loss-condition (Red Black) stalemated)	
)

(variant
    (title "Six Men's Morris")
	(board-setup
            (Red (man off 6))
            (Black (man off 6))
	)	
)

(variant
		(title "Nine Men's Morris")
	(default)
        (history "直棋的历史一般认为是起源于4世纪的罗马，但是也有说可以追溯到古埃及的。
                  直棋的流传十分广泛，斯里兰卡和冰岛都曾有出土过直棋的棋盘。
                  直棋流行的顶峰时期是中世纪，这个时期直棋开始在世界范围内流传。
                  中国民间俗称“走三棋”“打三棋”等。")
        (strategy "开局基本上决定了整局游戏。5、6子直棋先手优势很大，但还是需要考虑一些策略性。
                   开局成三不一定是好事，有可能因为难以移动而落后。形成双三（一个子来回移动连续成三）就是必胜了。\\
                   借助计算机分析，九子直棋的理论最佳结果是和棋。
                   但是如同4x4x4井字棋一样，9子直棋的计算量依然能和人脑相适应，不失为娱乐的一种好游戏。")

	    (board
            (image "images/Morris/9_Mens_Morris.bmp")
            (links n (a3 a2) (a2 a1) (e1 e2) (e2 e3) (d1 c1) (c1 b1)
			         (d2 c2) (c2 b2) (f1 g1) (g1 h1) (f2 g2) (g2 h2)
					 (f3 g3) (g3 h3) (d3 c3) (c3 b3)
		    )
            (links e (h1 a1) (a1 b1) (h2 a2) (a2 b2) (h3 a3) (a3 b3)
			         (g1 g2) (g2 g3) (c3 c2) (c2 c1) (f3 e3) 
					 (e3 d3) (f2 e2) (e2 d2) (f1 e1) (e1 d1)
		    )
		    (links s (a2 a3) (a1 a2) (e2 e1) (e3 e2) (c1 d1) (b1 c1)
			         (c2 d2) (b2 c2) (g1 f1) (h1 g1) (g2 f2) (h2 g2)
					 (g3 f3) (h3 g3) (c3 d3) (b3 c3)
		    )
            (links w (a1 h1) (b1 a1) (a2 h2) (b2 a2) (a3 h3) (b3 a3)
			         (g2 g1) (g3 g2) (c2 c3) (c1 c2) (e3 f3) 
					 (d3 e3) (e2 f2) (d2 e2) (e1 f1) (d1 e1)
		    )
			(links next (a1 a2) (a2 a3) (a3 b1) (b1 b2) (b2 b3) (b3 c1)
			         (c1 c2) (c2 c3) (c3 d1) (d1 d2) (d2 d3) 
					 (d3 e1) (e1 e2) (e2 e3) (e3 f1) (f1 f2)
					 (f2 f3) (f3 g1) (g1 g2) (g2 g3) (g3 h1)
					 (h1 h2) (h2 h3) (h3 last)
		    )
            (positions
				(positions-1-2)
				(positions-3)
				(last -200 -200 -100 -100)	
		   )
        )
        (board-setup
            (Red (man off 9))
            (Black (man off 9))
		)
)

(variant
    (title "Twelve Men's Morris")
    (history "12子直棋出现于14世纪左右的欧美。中国民间也有类似的“打三棋“。")
    (board
            (image "images/Morris/12_Mens_Morris.bmp")
            (links n (a3 a2) (a2 a1) (e1 e2) (e2 e3) (d1 c1) (c1 b1)
			         (d2 c2) (c2 b2) (f1 g1) (g1 h1) (f2 g2) (g2 h2)
					 (f3 g3) (g3 h3) (d3 c3) (c3 b3)
		    )
            (links e (h1 a1) (a1 b1) (h2 a2) (a2 b2) (h3 a3) (a3 b3)
			         (g1 g2) (g2 g3) (c3 c2) (c2 c1) (f3 e3) 
					 (e3 d3) (f2 e2) (e2 d2) (f1 e1) (e1 d1)
		    )
		    (links s (a2 a3) (a1 a2) (e2 e1) (e3 e2) (c1 d1) (b1 c1)
			         (c2 d2) (b2 c2) (g1 f1) (h1 g1) (g2 f2) (h2 g2)
					 (g3 f3) (h3 g3) (c3 d3) (b3 c3)
		    )
            (links w (a1 h1) (b1 a1) (a2 h2) (b2 a2) (a3 h3) (b3 a3)
			         (g2 g1) (g3 g2) (c2 c3) (c1 c2) (e3 f3) 
					 (d3 e3) (e2 f2) (d2 e2) (e1 f1) (d1 e1)
		    )
			(links out (b1 b2) (b2 b3) 
			           (d1 d2) (d2 d3) 
					   (f1 f2) (f2 f3)
			           (h1 h2) (h2 h3)
		    )
			(links in (b2 b1) (b3 b2)
			          (d2 d1) (d3 d2) 
					  (f2 f1) (f3 f2)
			          (h2 h1) (h3 h2)
		    )
			(links next (a1 a2) (a2 a3) (a3 b1) (b1 b2) (b2 b3) (b3 c1)
			         (c1 c2) (c2 c3) (c3 d1) (d1 d2) (d2 d3) 
					 (d3 e1) (e1 e2) (e2 e3) (e3 f1) (f1 f2)
					 (f2 f3) (f3 g1) (g1 g2) (g2 g3) (g3 h1)
					 (h1 h2) (h2 h3) (h3 last)
		    )
            (positions   
				(positions-1-2)
				(positions-3)
				(last -200 -200 -100 -100)	
		)
    )
	(piece
              (name man)
		      (image Red   "images/Morris/NMMRedZ.bmp"   "images/Morris/NMMRed.bmp"
		             Black "images/Morris/NMMBlackZ.bmp" "images/Morris/NMMBlack.bmp")
		      (help "Man: First drop to any square on the board, then move along the lines.")
              (drops
                (move-type dropping)
                (add-to-empty2)
              )
		      (moves
		         (move-type moving)
		         (shiftn2 n)
		         (shifte2 e)
		         (shiftn2 s)
		         (shifte2 w)
				 (shifti2 in)
				 (shifti2 out)
              )
     )

	(board-setup
            (Red (man off 12))
            (Black (man off 12))
	)
)